﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GraphicLib;
using System.IO;

namespace GraphicIdentification
{
    public partial class frmGrapicIdentification : Form
    {
        public frmGrapicIdentification()
        {
            InitializeComponent();
            //界面上有个初始化按钮，比较值，输出
            input_splitcount.Text = ProductCheck.SPLIT_COUNT.ToString();
            input_max_splitcount.Text = ProductCheck.SPLIT_MAX_COUNT.ToString();
            input_tolerance.Text = ProductCheck.VARIANCE_TOLERANCE.ToString();

            SamplesInition.DeserializeSample();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            try
            {
                ProductCheck.SPLIT_COUNT = Convert.ToInt32(input_splitcount.Text);
                txtLog.AppendText(string.Format("{0} 开始初始化样品图像...\r\n",DateTime.Now));
                SamplesInition.InitSamples();
                txtLog.AppendText(string.Format("{0} 初始化样品图像完成!\r\n",DateTime.Now));
            }
            catch (Exception ex)
            {
                txtLog.AppendText(ex.Message);
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(txtProductsDir.Text))
                {
                    MessageBox.Show("请选择产品目录");
                    return;
                }
                btnStart.Enabled = false;
                //坏品自学习
                bad_product_learning();
                //检测产品，并输出日志                
                txtLog.AppendText(string.Format("---- {0} 开始产品检测 ----\r\n", DateTime.Now));

                //ProductCheck.CheckProducts(txtLog);
                ProcessThread processTread = new ProcessThread();
                processTread.StartAProcessThread(txtLog);
                btnStart.Enabled = false;
                btnInit.Enabled = true;
            }
            catch (Exception ex)
            {
                txtLog.AppendText(ex.Message);
            }
        }

        private void btn_parm_save_Click(object sender, EventArgs e)
        {
            ProductCheck.SPLIT_COUNT = Convert.ToInt32(input_splitcount.Text);
            ProductCheck.VARIANCE_TOLERANCE = Convert.ToDouble(input_tolerance.Text);
            MessageBox.Show("参数保存成功！请重新初始化！");
        }
        private void bad_product_learning()
        { 
            string processFolder=System.Environment.CurrentDirectory + @"\Processing_Pool";
            string badProductFilePath = System.Environment.CurrentDirectory + @"\Bad_Products";
            string[] files = Directory.GetFiles(processFolder);
            if (files.Length>0)
            {
                foreach (string file in files)
                {
                    try
                    {
                        Image img = Image.FromFile(file);
                        img.Dispose();
                        ProductCheck.SPLIT_COUNT += 1;
                        input_splitcount.Text = ProductCheck.SPLIT_COUNT.ToString();
                        SamplesInition.InitSamples();
                        string fileName = file.Substring(file.LastIndexOf(@"\") + 1);
                        File.Move(file, badProductFilePath + "\\"+fileName);                      
                    }
                    catch (Exception ex)
                    {
                        File.AppendAllText(Sample.LOG_PATH, ex.Message);
                    }
                }
            }
        }

        private void btnSampleLearn_Click(object sender, EventArgs e)
        {
            SamplesInition.LearnNewSamples();
        }

        private void btnChooseProductsDir_Click(object sender, EventArgs e)
        {
            ChooseProductsDir();
        }

        private void txtProductsDir_Click(object sender, EventArgs e)
        {
            ChooseProductsDir();
        }

        private void ChooseProductsDir()
        {
            FolderBrowserDialog productsDirChooseDialog = new FolderBrowserDialog();
            productsDirChooseDialog.Description = "请选择产品图像所在目录";
            if (productsDirChooseDialog.ShowDialog() == DialogResult.OK)
                txtProductsDir.Text = productsDirChooseDialog.SelectedPath;
            productsDirChooseDialog.Dispose();
            Sample.PRODUCT_DIR = txtProductsDir.Text;
        }
    }
}
